Ext.define('Monkey.view.BaseController', {
    extend: 'Ext.app.ViewController',

    init: function () {
        var me = this;
        var components = Ext.ComponentQuery.query('component[permission]');
        console.log(components);
        if (components.length > 0) {
            me.handlerPermission(components);
        }
    },

    handlerPermission: function (components) {
        var me = this;
        var view = me.getView();
        var user = Ext.decode(Ext.util.Cookies.get(Monkey.Config.user));
        console.log(user.permissions);
        for (var i = 0; i < components.length; i++) {
            var cmp = components[i];
            var needPermission = cmp.permission;
            if (!me.hasPermission(needPermission, user.permissions)) {
                //cmp.hide();
                cmp.disable();
            }
        }
    },

    hasPermission: function (needPermission, permissions) {
        for (var i = 0; i < permissions.length; i++) {
            var permission = permissions[i];
            if (needPermission == permission) {
                return true;
            }
            var index = permission.lastIndexOf('*');
            if (index != -1) {
                var start = permission.substring(0, index);
                if (needPermission.indexOf(start) == 0) {
                    return true;
                }
            }
        }
        return false;
    },

    request: function (url, method, params, callback) {
        Monkey.Utils.request(url, method, params, callback);
    },

    toLoginPage: function () {
        Ext.getCmp('mainView').destroy();
        Ext.util.Cookies.clear(Monkey.Config.user);
        Ext.util.Cookies.clear(Monkey.Config.loggedIn);
        Ext.create('Monkey.view.authentication.Login');
    },

    winCancel: function () {
        this.getView().close();
    },


    //常用的渲染方法
    renderBoolean: function (value) {
        return value ? '<div class="x-tree-icon accept"></div>' : '<div class=" x-tree-icon delete"></div>';
    },

    renderStatus: function (value) {
        //return value == 'ENABLE' ? '启用' : '禁用';
        return value == 'ENABLE' ? '<div class="x-tree-icon accept"></div>' : '<div class=" x-tree-icon delete"></div>';
    }


});
